﻿package com.mdp.input 
{
	import flash.events.MouseEvent;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import com.mdp.controllers.MovementController;
	import flash.geom.Rectangle;
	import flash.display.StageDisplayState;
	
	/**
	 * ...
	 * @author ...
	 */
	public class MainInput 
	{
		
		static public var allowInput:Boolean;
		
		protected var _stage:Stage;
		
		
		public function MainInput(stage:Stage) 
		{
			//store stage ref
			_stage = stage;
			
			//input allowed?
			allowInput = true;
			
			//add event listeners
			//_stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			_stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			_stage.addEventListener( MouseEvent.DOUBLE_CLICK, doFull );
		}
		
		public function enableInput( ):void {
		   allowInput = true;
	   }
		
	   public function doFull( e:MouseEvent = null ):void {
			var fullScreenRect:Rectangle = new Rectangle(0, 0, 800, 600);

			// calculate aspect ratio of the red square
			var rectAspectRatio:Number = fullScreenRect.width / fullScreenRect.height;

			// calculate aspect ratio of the screen
			var screenAspectRatio:Number = _stage.fullScreenWidth / _stage.fullScreenHeight;

			// change the fullScreenRect so that it covers the entire screen, keeping it centered on the redSquare
			// try commenting out this section to see what happens if you do not fix the aspect ratio.
			if (rectAspectRatio > screenAspectRatio) {
			 var newHeight:Number = fullScreenRect.width / screenAspectRatio;
			 fullScreenRect.y -= ((newHeight - fullScreenRect.height) / 2);
			fullScreenRect.height = newHeight;
			} else if (rectAspectRatio < screenAspectRatio) {
			var newWidth:Number = fullScreenRect.height * screenAspectRatio;
			fullScreenRect.x -= ((newWidth - fullScreenRect.width) / 2);
			fullScreenRect.width = newWidth;
			}

			// go to full screen
			_stage.fullScreenSourceRect = fullScreenRect;
			_stage.displayState = StageDisplayState.FULL_SCREEN;

	   }
	   
	   static public function enableInput():void {
		   allowInput = true;
	   }
	   
		protected function onKeyDown(e:KeyboardEvent):void {
			if (allowInput) {
				MovementController.handleMovement(e);
			}
		}
		
	}
	
}